package com.ray.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.ray.util.qiniu.QiniuConfig;
import com.ray.util.qiniu.QiniuUploadUtils;

/**
 * 文件上传
 * @author Shensg
 * 2018年1月24日
 */
public class UploadFilesUtil {
	
	public static List<String> loadFileUtils(CommonsMultipartFile files[],HttpServletRequest request,String address) throws IOException{
		BufferedInputStream bufferedInput = null;  
    	BufferedOutputStream bufferedOutput = null;
    	FileOutputStream fos=null;
    	InputStream in = null;
        List<String> list = new ArrayList<String>();  
        // 获取项目的路径  
        ServletContext sc = request.getSession().getServletContext();  
        // 上传位置  
        String path = sc.getRealPath("/") + "/"; // 设定文件保存的目录  
        File ff = new File(path);  
        path=ff.getParent()+address+"/";
        File f = new File(path);  
        if (!f.exists())  
            f.mkdirs();        
        for (int i = 0; i < files.length; i++) {  
            // 获取原始文件名
            String fileName = files[i].getOriginalFilename();  
            System.out.println("原始文件名:" + fileName);  
            // 新文件名  
            String newFileName = UUID.randomUUID() + fileName;  
            if (!files[i].isEmpty()) {  
                try {   
                	byte[] buffer = new byte[1024];  
                     fos = new FileOutputStream(path  
                            + newFileName);  
                    bufferedOutput = new BufferedOutputStream(fos);  
                    in = files[i].getInputStream(); 
                    bufferedInput = new BufferedInputStream(in);  
                    int copySize;  
                    while ((copySize = bufferedInput.read(buffer)) > 0){  
                    	bufferedOutput.write(buffer, 0, copySize);  
                    }
                } catch (Exception e) {  
                    e.printStackTrace();  
                } finally{
                	bufferedOutput.flush();  
                    bufferedOutput.close();  
                    fos.close();  
                    in.close();  
                }
            }  
            System.out.println("上传图片到:" + path + newFileName);  
            list.add(address+"/"+newFileName);  
        }
        return list;
	}
	/**
	 * 七牛 bucketShinehaopic
	 * @param files
	 * @param request
	 * @return
	 * @throws IOException
	 */
	public static List<String> loadFileByQiniu4BucketShinehaopic(CommonsMultipartFile files[],HttpServletRequest request) throws IOException{
    	InputStream in = null;
        List<String> list = new ArrayList<String>();  
        for (int i = 0; i < files.length; i++) {  
            // 获取原始文件名
            String fileName = files[i].getOriginalFilename();  
            System.out.println("原始文件名:" + fileName);  
            if (!files[i].isEmpty()) {  
                try {     
                    in = files[i].getInputStream(); 
                    list.add(QiniuUploadUtils.uploadFileByStream(QiniuConfig.accessKey, QiniuConfig.secretKey, QiniuConfig.bucketShinehaopic, in, fileName,QiniuConfig.bucketShinehaopicUrlPrefix));
                } catch (Exception e) {  
                    e.printStackTrace();  
                } finally{
                    in.close();  
                }
            }  
        }
        return list;
	}
	/**
	 * 保存base64文件
	 * @param base64content
	 * @param request
	 * @return
	 * @throws IOException
	 */
	public static List<String> loadBase64FileByQiniu4BucketShinehaopic(String base64Content,String fileName,HttpServletRequest request) throws IOException{
		List<String> list = new ArrayList<String>();  
		list.add(QiniuUploadUtils.uploadFileByBase64(base64Content,QiniuConfig.accessKey, QiniuConfig.secretKey, QiniuConfig.bucketShinehaopic,  QiniuConfig.bucketShinehaopicUrlPrefix));
		return list;
	}

}
